package com.hxkcloud.nurse.popup;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.MonitorKindBean;
import com.hxkcloud.nurse.beans.NoticeFunctionBean;
import com.hxkcloud.nurse.dborm.MonitorKindDao;
import com.hxkcloud.nurse.utils.BrightnessUtils;
import com.hxkcloud.nurse.wheelview.WheelView;
import com.hxkcloud.nurse.wheelview.adapter.ArrayWheelAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by william_sun on 2016-07-12.
 * Description:选择提醒类型（血压血糖文字文章）
 */
public class NoticeTypeSelectPopWindow extends PopupWindow implements View.OnClickListener {

    private Activity context;
    private WheelView wheel_addr;

    private Button bt_cancel;
    private Button bt_sure;

    //old0测量血压 1测量血糖 2提醒文字 3提醒文章
    private ArrayList<NoticeFunctionBean> functions;

//    private List<MonitorKindBean> monitors;

    public NoticeTypeSelectPopWindow(Activity context) {
        this.context = context;
        View rootView = LayoutInflater.from(context).inflate(R.layout.popup_selectaddress, null);
        setContentView(rootView);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(0x00000000));

        setAnimationStyle(R.style.popupwindow_anim_default);
        initViews(rootView);
        MonitorKindDao monitorKindDao = new MonitorKindDao(context);
        List<MonitorKindBean> monitors = monitorKindDao.getKinds();
        initDatas(monitors);
        initListener();
    }

    private void initViews(View rootView) {
        wheel_addr = (WheelView) rootView.findViewById(R.id.wheel_selectaddress_add);
        bt_cancel = (Button) rootView.findViewById(R.id.bt_selectaddress_cancel);
        bt_sure = (Button) rootView.findViewById(R.id.bt_selectaddress_sure);
//        wheel_addr.setDrawShadows(false);
//        wheel_addr.setBackgroundColor(ContextCompat.getColor(context, R.color.white));

    }

    public void initDatas(List<MonitorKindBean> monitors) {

//        NumericWheelAdapter numericWheelAdapter1 = new NumericWheelAdapter(context, 1970, curYear);
//        numericWheelAdapter1.setLabel(context.getString(R.string.pop_timeSelector_year));
//        wheel_year.setViewAdapter(numericWheelAdapter1);
//        wheel_year.setCyclic(true);//是否可循环滑动
//
//        NumericWheelAdapter numericWheelAdapter2 = new NumericWheelAdapter(context, 1, 12, "%02d");
//        numericWheelAdapter2.setLabel(context.getString(R.string.pop_timeSelector_month));
//        wheel_month.setViewAdapter(numericWheelAdapter2);
//        wheel_month.setCyclic(true);

//        MonitorKindDao monitorKindDao = new MonitorKindDao(context);
//        monitors = monitorKindDao.getKinds();
        functions = new ArrayList<>();
        for (int i = 0; i < monitors.size(); i++) {
            NoticeFunctionBean bean = new NoticeFunctionBean();
            bean.setItemId(i + "");
            bean.setContent(monitors.get(i).getName());
            functions.add(bean);
        }


        NoticeFunctionBean beanW = new NoticeFunctionBean();
        beanW.setItemId(monitors.size() + "");
        beanW.setContent("提醒文字");
        NoticeFunctionBean beanA = new NoticeFunctionBean();
        beanA.setItemId((monitors.size() + 1) + "");
        beanA.setContent("提醒文章");
        functions.add(beanW);
        functions.add(beanA);

        String[] fs = new String[functions.size()];
        for (int i = 0; i < functions.size(); i++) {
            fs[i] = functions.get(i).getContent();
        }
        ArrayWheelAdapter adapter = new ArrayWheelAdapter(context, fs);
        wheel_addr.setViewAdapter(adapter);
        wheel_addr.setCyclic(false);

        //设置显示行数
        wheel_addr.setVisibleItems(5);

    }

    private void initListener() {
        bt_sure.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(context);
            }
        });
    }

    @Override
    public void onClick(View v) {
        dismiss();
        switch (v.getId()) {
            case R.id.bt_selectaddress_cancel:

                break;
            case R.id.bt_selectaddress_sure:
                if (typeListener != null) {
                    typeListener.setNoticeType(functions.get(wheel_addr.getCurrentItem()).getItemId());
                }
                break;
        }
    }

    public void show(View parent) {
        showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        BrightnessUtils.lightOff(context);
    }

    public interface NoticeTypeListener {
        void setNoticeType(String type);
    }

    private NoticeTypeListener typeListener;

    public void setNoticeTypeListener(NoticeTypeListener typeListener) {
        this.typeListener = typeListener;
    }
}
